package com.fireline.beauty.member;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.widgets.ControlUtil;

import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.widgets.Label;

public class FwxmSearchDialog extends Dialog {

	protected Object result;
	protected Shell shell;
	private Text text;
	private Table table;
	private Text text_1;
	/**
	 * @wbp.nonvisual location=27,482
	 */
	public final Dataset fwxmSet = new Dataset(true, "fwxmcz",
			SqliteJdbcImpl.INSTANCE);

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public FwxmSearchDialog(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		ControlUtil.centerShell(shell);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setSize(400, 422);
		shell.setText("服务项目查找");

		GridLayout gl_content = new GridLayout();
		gl_content.marginBottom = 5;
		gl_content.marginTop = 10;
		gl_content.verticalSpacing = 10;
		gl_content.marginWidth = 10;
		gl_content.marginHeight = 0;
		gl_content.horizontalSpacing = 0;
		shell.setLayout(gl_content);

		text_1 = new Text(shell, SWT.BORDER);
		text_1.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				doFilter(text_1.getText());
			}
		});
		text_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		text_1.setMessage("在些输入项目名称或编号进行查找");
		text_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);

		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				if (table.getSelectionCount() > 0) {
					okPressed();
				}
			}
		});
		GridData gd_table = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_table.heightHint = 0;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);

		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(70);
		tblclmnNewColumn.setText("项目编号");

		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(143);
		tblclmnNewColumn_1.setText("项目名称");

		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.RIGHT);
		tblclmnNewColumn_2.setWidth(60);
		tblclmnNewColumn_2.setText("单价");

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);
		gd_composite.verticalIndent = 5;
		gd_composite.horizontalIndent = -1;
		composite.setLayoutData(gd_composite);

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblNewLabel.setText("注：按住Ctrl键可选择多条记录");

		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				okPressed();
			}
		});
		GridData gd_btnNewButton_1 = new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1);
		gd_btnNewButton_1.heightHint = 25;
		gd_btnNewButton_1.widthHint = 80;
		btnNewButton_1.setLayoutData(gd_btnNewButton_1);
		btnNewButton_1.setText("确定");

		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		GridData gd_btnNewButton = new GridData(SWT.LEFT, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 80;
		gd_btnNewButton.heightHint = 25;
		btnNewButton.setLayoutData(gd_btnNewButton);
		btnNewButton.setText("关闭");
		fwxmSet.update();
		doFilter("");
	}

	public void doFilter(String f) {
		table.removeAll();
		for (int i = 0; i < fwxmSet.getCount(); i++) {
			fwxmSet.setSelected(i);
			String xmbh = fwxmSet.getString("xmbh");
			String xmmc = fwxmSet.getString("xmmc");
			String jm = fwxmSet.getString("jm");
			String dj = fwxmSet.getString("dj");
			if (xmbh != null && xmbh.contains(f)
					|| (xmmc != null && xmmc.contains(f))
					|| (jm != null && jm.contains(f))) {
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(new String[] { xmbh, xmmc, dj });
				item.setData(i);
			}
		}
		if (table.getItemCount() > 0)
			table.setSelection(0);
	}

	protected void okPressed() {
		if (table.getSelectionCount() > 0) {
			int[] indexs = new int[table.getSelectionCount()];
			int i = 0;
			for (TableItem item : table.getSelection()) {
				indexs[i++] = (Integer) item.getData();
			}
			result = indexs;
			shell.close();
		} else {
			MessageDialog.openInformation(shell, "提示", "请选择一条记录");
		}
	}

}
